package org.stone.project.tx.api;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.stone.project.tx.pojo.dto.Result;
import org.stone.project.tx.pojo.vo.MessageVo;
import org.stone.project.tx.service.MsgService;
import org.stone.project.tx.sysEnum.MsgType;

import javax.validation.Valid;

@RestController
@RequestMapping("msg")
public class MessageController {
    @Autowired
    private MsgService msgService;

    /**
     * 同步发送消息
     * @param messageVo
     * @return
     */
    @PostMapping("sendMsg")
    public Result sendMsg(@Valid @RequestBody MessageVo messageVo){
        MsgType msgType = MsgType.getInstance(messageVo.getContentType());
        if (ObjectUtil.isNull(msgType))
            throw new IllegalArgumentException("contentType不能为空");
        msgService.sendMsg(messageVo);
        return Result.ok();
    }

    /**
     * 异步发送消息
     * @param messageVo
     * @return
     */
    @PostMapping("sendMsgSync")
    public Result sendMsgSync(@Valid @RequestBody MessageVo messageVo){
        MsgType msgType = MsgType.getInstance(messageVo.getContentType());
        if (ObjectUtil.isNull(msgType))
            throw new IllegalArgumentException("contentType不能为空");
        msgService.sendMsgSync(messageVo);
        return Result.ok();
    }
}
